{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9.11 \u88c5\u9970\u5668\u4e3a\u88ab\u5305\u88c5\u51fd\u6570\u589e\u52a0\u53c2\u6570\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u88c5\u9970\u5668\u4e2d\u7ed9\u88ab\u5305\u88c5\u51fd\u6570\u589e\u52a0\u989d\u5916\u7684\u53c2\u6570\uff0c\u4f46\u662f\u4e0d\u80fd\u5f71\u54cd\u8fd9\u4e2a\u51fd\u6570\u73b0\u6709\u7684\u8c03\u7528\u89c4\u5219\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u6765\u7ed9\u88ab\u5305\u88c5\u51fd\u6570\u589e\u52a0\u989d\u5916\u53c2\u6570\u3002\u8003\u8651\u4e0b\u9762\u7684\u88c5\u9970\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import wraps\n\ndef optional_debug(func):\n    @wraps(func)\n    def wrapper(*args, debug=False, **kwargs):\n        if debug:\n            print('Calling', func.__name__)\n        return func(*args, **kwargs)\n\n    return wrapper"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@optional_debug\ndef spam(a,b,c):\n    print(a,b,c)\nspam(1,2,3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam(1,2,3, debug=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u88c5\u9970\u5668\u6765\u7ed9\u88ab\u5305\u88c5\u51fd\u6570\u589e\u52a0\u53c2\u6570\u7684\u505a\u6cd5\u5e76\u4e0d\u5e38\u89c1\u3002\n\u5c3d\u7ba1\u5982\u6b64\uff0c\u6709\u65f6\u5019\u5b83\u53ef\u4ee5\u907f\u514d\u4e00\u4e9b\u91cd\u590d\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e0b\u9762\u8fd9\u6837\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def a(x, debug=False):\n    if debug:\n        print('Calling a')\n\ndef b(x, y, z, debug=False):\n    if debug:\n        print('Calling b')\n\ndef c(x, y, debug=False):\n    if debug:\n        print('Calling c')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u90a3\u4e48\u4f60\u53ef\u4ee5\u5c06\u5176\u91cd\u6784\u6210\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import wraps\nimport inspect\n\ndef optional_debug(func):\n    if 'debug' in inspect.getargspec(func).args:\n        raise TypeError('debug argument already defined')\n\n    @wraps(func)\n    def wrapper(*args, debug=False, **kwargs):\n        if debug:\n            print('Calling', func.__name__)\n        return func(*args, **kwargs)\n    return wrapper\n\n@optional_debug\ndef a(x):\n    pass\n\n@optional_debug\ndef b(x, y, z):\n    pass\n\n@optional_debug\ndef c(x, y):\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u5b9e\u73b0\u65b9\u6848\u4e4b\u6240\u4ee5\u884c\u5f97\u901a\uff0c\u5728\u4e8e\u5f3a\u5236\u5173\u952e\u5b57\u53c2\u6570\u5f88\u5bb9\u6613\u88ab\u6dfb\u52a0\u5230\u63a5\u53d7 *args \u548c **kwargs \u53c2\u6570\u7684\u51fd\u6570\u4e2d\u3002\n\u901a\u8fc7\u4f7f\u7528\u5f3a\u5236\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5b83\u88ab\u4f5c\u4e3a\u4e00\u4e2a\u7279\u6b8a\u60c5\u51b5\u88ab\u6311\u9009\u51fa\u6765\uff0c\n\u5e76\u4e14\u63a5\u4e0b\u6765\u4ec5\u4ec5\u4f7f\u7528\u5269\u4f59\u7684\u4f4d\u7f6e\u548c\u5173\u952e\u5b57\u53c2\u6570\u53bb\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u8fd9\u4e2a\u7279\u6b8a\u53c2\u6570\u4f1a\u88ab\u6392\u9664\u5728\u5916\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u5e76\u4e0d\u4f1a\u88ab\u7eb3\u5165\u5230 **kwargs \u4e2d\u53bb\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e00\u4e2a\u96be\u70b9\u5c31\u662f\u5982\u4f55\u53bb\u5904\u7406\u88ab\u6dfb\u52a0\u7684\u53c2\u6570\u4e0e\u88ab\u5305\u88c5\u51fd\u6570\u53c2\u6570\u76f4\u63a5\u7684\u540d\u5b57\u51b2\u7a81\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u88c5\u9970\u5668 @optional_debug \u4f5c\u7528\u5728\u4e00\u4e2a\u5df2\u7ecf\u62e5\u6709\u4e00\u4e2a debug \u53c2\u6570\u7684\u51fd\u6570\u4e0a\u65f6\u4f1a\u6709\u95ee\u9898\u3002\n\u8fd9\u91cc\u6211\u4eec\u589e\u52a0\u4e86\u4e00\u6b65\u540d\u5b57\u68c0\u67e5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u7684\u65b9\u6848\u8fd8\u53ef\u4ee5\u66f4\u5b8c\u7f8e\u4e00\u70b9\uff0c\u56e0\u4e3a\u7cbe\u660e\u7684\u7a0b\u5e8f\u5458\u5e94\u8be5\u53d1\u73b0\u4e86\u88ab\u5305\u88c5\u51fd\u6570\u7684\u51fd\u6570\u7b7e\u540d\u5176\u5b9e\u662f\u9519\u8bef\u7684\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@optional_debug\ndef add(x,y):\n    return x+y\nimport inspect\nprint(inspect.signature(add))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u5982\u4e0b\u7684\u4fee\u6539\uff0c\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import wraps\nimport inspect\n\ndef optional_debug(func):\n    if 'debug' in inspect.getargspec(func).args:\n        raise TypeError('debug argument already defined')\n\n    @wraps(func)\n    def wrapper(*args, debug=False, **kwargs):\n        if debug:\n            print('Calling', func.__name__)\n        return func(*args, **kwargs)\n\n    sig = inspect.signature(func)\n    parms = list(sig.parameters.values())\n    parms.append(inspect.Parameter('debug',\n                inspect.Parameter.KEYWORD_ONLY,\n                default=False))\n    wrapper.__signature__ = sig.replace(parameters=parms)\n    return wrapper"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u8fd9\u6837\u7684\u4fee\u6539\uff0c\u5305\u88c5\u540e\u7684\u51fd\u6570\u7b7e\u540d\u5c31\u80fd\u6b63\u786e\u7684\u663e\u793a debug \u53c2\u6570\u7684\u5b58\u5728\u4e86\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@optional_debug\ndef add(x,y):\n    return x+y\nprint(inspect.signature(add))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "add(2,3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53c2\u80039.16\u5c0f\u8282\u83b7\u53d6\u66f4\u591a\u5173\u4e8e\u51fd\u6570\u7b7e\u540d\u7684\u4fe1\u606f\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}